package guestbook;

import com.google.appengine.api.datastore.*;
import java.util.*;

public class LPreparedQuery implements PreparedQuery
{
	public final PreparedQuery pq;
	public final Context context;
	public final Query query;

	public LPreparedQuery(PreparedQuery pq, Context context, Query query)
	{
		this.pq = pq;
		this.query = query;
		this.context = context;
	}

	public void tallyCall(String method)
	{
		context.tally.tallyCall(query.getKind() + "-" + method);
	}

	public ConsistentResults asList()
	{
		return asList(FetchOptions.Builder.withDefaults());
	}
	
	@Override
	public ConsistentResults asList(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asList");
		
		return new ConsistentResults(context, pq.asList(fetchOptions), query);
	}
	
	@Override
	public Iterable<Entity> asIterable()
	{
		if (Tally.ON)
			tallyCall("asIterable");

		return asList();
	}

	@Override
	public Iterable<Entity> asIterable(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asIterable");

		return asList();
	}

	@Override
	public Iterator<Entity> asIterator()
	{
		if (Tally.ON)
			tallyCall("asIterator");

		return asList();
	}

	@Override
	public Iterator<Entity> asIterator(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asIterator");

		return asList(fetchOptions);
	}

	@Override
	public QueryResultIterable<Entity> asQueryResultIterable()
	{
		if (Tally.ON)
			tallyCall("asQueryResultIterable");
		
		return pq.asQueryResultIterable();
	}

	@Override
	public QueryResultIterable<Entity> asQueryResultIterable(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asQueryResultIterable");
		
		return pq.asQueryResultIterable(fetchOptions);
	}

	@Override
	public QueryResultIterator<Entity> asQueryResultIterator()
	{
		if (Tally.ON)
			tallyCall("asQueryResultIterator");
		
		return pq.asQueryResultIterator();
	}

	@Override
	public QueryResultIterator<Entity> asQueryResultIterator(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asQueryResultIterator");
		
		return pq.asQueryResultIterator(fetchOptions);
	}

	@Override
	public QueryResultList<Entity> asQueryResultList(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("asQueryResultList");
		
		return pq.asQueryResultList(fetchOptions);
	}

	@Override
	public Entity asSingleEntity() throws TooManyResultsException
	{
		if (Tally.ON)
			tallyCall("asSingleEntity");

		return pq.asSingleEntity();
	}

	@SuppressWarnings("deprecation")
	@Override
	public int countEntities()
	{
		if (Tally.ON)
			tallyCall("countEntities");

		return pq.countEntities();
	}

	@Override
	public int countEntities(FetchOptions fetchOptions)
	{
		if (Tally.ON)
			tallyCall("countEntities");

		return pq.countEntities(fetchOptions);
	}
}